package main

func main() {

	//root = [4,2,9,3,5,null,7]
	node3 := TreeNode{
		Val:   3,
		Left:  nil,
		Right: nil,
	}
	node4 := TreeNode{
		Val:   5,
		Left:  nil,
		Right: nil,
	}
	node5 := TreeNode{
		Val:   7,
		Left:  nil,
		Right: nil,
	}
	node1 := TreeNode{
		Val:   2,
		Left:  &node3,
		Right: &node4,
	}
	node2 := TreeNode{
		Val:   9,
		Left:  nil,
		Right: &node5,
	}
	root := TreeNode{
		Val:   4,
		Left:  &node1,
		Right: &node2,
	}

	//打印测试用例返回结果
	resp := isSymmetric(&root)
	println(resp)

}

//Definition for a binary tree node.
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func isSymmetric(root *TreeNode) bool {
	//边界条件
	if root.Left == nil && root.Right == nil {
		return true
	}
	if root.Left == nil || root.Right == nil { //只有单节点，就是false
		return false
	}
	//拆分成左半树和右半树。对两树对比对称情况
	return judgeSymmetric(root.Left, root.Right)
}
func judgeSymmetric(p *TreeNode, q *TreeNode) bool {

	//p和q只有其中一个为空，返回false
	if p != nil && q == nil || p == nil && q != nil {
		return false
	}
	if p == nil && q == nil {
		return true
	}
	if p.Val != q.Val {
		return false
	}
	if !judgeSymmetric(p.Left, q.Right) {
		return false
	}
	if !judgeSymmetric(p.Right, q.Left) {
		return false
	}
	return true
}
